#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.module.user.controller;

import ${package}.basic.module.user.service.UserService;
import ${package}.common.bean.po.User;
import ${package}.common.bean.vo.ApiUser;
import ${package}.common.bean.vo.DisableOrEnableUserVo;
import ${package}.common.bean.vo.JsonResult;
import ${package}.common.bean.vo.QueryBean;
import ${package}.common.bean.vo.UpdatePasswordVo;
import ${package}.common.bean.vo.UpdateUserVo;
import ${package}.common.bean.vo.UserVo;
import ${package}.common.emum.ConstEnum;
import ${package}.common.emum.LoginEnum;
import ${package}.common.emum.ReturnCodeEnum;
import ${package}.common.utils.RsaUtil;
import ${package}.common.utils.SessionUtil;
import ${package}.common.utils.SnowFlakeUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Description: UserController
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
@RestController
@RequestMapping("/yk_api/user/")
@AllArgsConstructor
@Api(value = "用户")
public class UserController {

    /**
     * 用户Service
     */
    private final UserService userService;

    private final PasswordEncoder passwordEncoder;

    /**
     * Description:
     * <刷新权限信息>
     * @author ywh
     * @date 11:19 2019/8/7
     * @return JsonResult
     **/
    @GetMapping("refreshPermiss")
    @ApiOperation(value = "刷新权限信息",notes = "刷新权限信息")
    public JsonResult<Boolean> refreshPermiss(){
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, Boolean.TRUE);
    }

    /**
     * Description:
     * <获得所有用户>
     * @author wupanhua
     * @date 11:20 2019/8/7
     * @return java.util.List<User>
     **/
    @GetMapping("findAllUser")
    @ApiOperation(value="获取用户列表",notes = "获取用户列表")
    public List<User> findAllUser(){
        return userService.findAllUser();
    }

	@Transactional(rollbackFor = Exception.class)
	@PostMapping("password")
	public JsonResult<String> updatePassword(@RequestBody @Validated UpdatePasswordVo userVo) {
		User user = SessionUtil.getUser();
		UpdateUserVo updateUserVo = new UpdateUserVo();
		updateUserVo.setId(user.getId());
		if (!passwordEncoder.matches(userVo.getOldPassWord(), user.getPassword())) {
			return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, "原密码错误");
		} else {
			updateUserVo.setPassword(passwordEncoder.encode(userVo.getPassword()));
		}

		userService.updateUserById(updateUserVo);
		return JsonResult.getInstant(ReturnCodeEnum.SUCCESS);
	}

    /**
     * Description:
     * <使用邮件和密码登录>
     * @author wupanhua
     * @date 11:21 2019/8/7
     * @param user 1
     * @return JsonResult<User>
     **/
    @PostMapping("findByEmail")
    @ApiOperation(value = "使用邮件和密码登录",notes = "使用邮件和密码登录")
    @ApiImplicitParam(name = "user",value = "用户实体类",dataType = "User",paramType = "body",required = true)
    public JsonResult<User> loginByUserEmail(User user){

        user = userService.findUserByEamilAndPassword(user);

        // 返回数据
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, user);
    }

    /**
     * Description:
     * <登出系统>
     * @author wupanhua
     * @date 11:21 2019/8/7
     * @return JsonResult<User>
     **/
    @GetMapping("logout")
    @ApiOperation(value = "登出系统",notes = "登出系统")
    public JsonResult<String> logout(){

        SessionUtil.removeSession();
        return JsonResult.getInstant(LoginEnum.LOGOUT);
    }

    /**
     * Description:
     * <获取用户列表>
     * @author wupanhua
     * @date 11:21 2019/8/7
     * @param queryBean 1
     * @return JsonResult
     **/
    @PostMapping("userList")
    @ApiOperation(value = "获取用户列表",notes = "获取用户列表")
    @ApiImplicitParam(name = "queryBean",value = "查询条件",dataType = "QueryBean",paramType = "body",required = true)
    public JsonResult<PageInfo<User>> queryUserList(@RequestBody @Valid QueryBean queryBean){

        PageInfo<User> page = userService.queryUserListByLimit(queryBean);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, page);
    }

    /**
     * Description:
     * <新增用户>
     * @author wupanhua
     * @date 11:22 2019/8/7
     * @param userVo 1
     * @return JsonResult
     **/
    @PostMapping("addUser")
    @ApiOperation(value = "新增用户",notes = "新增用户")
    @ApiImplicitParam(name = "UserVo",value = "新增用户实体类",dataType = "UserVo",paramType = "body",required = true)
    public JsonResult<User> addUser(@RequestBody @Valid UserVo userVo){

        // 拷贝数据,加密密码
        User user = new User();
        BeanUtils.copyProperties(userVo, user);
        user.setId(SnowFlakeUtil.getID());
        user.setPassword(passwordEncoder.encode(ConstEnum.DEFAULT_PASSWORD.getNickName()));

        // 插入数据
        userService.addUser(user);
        // 返回数据
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, user);
    }

    /**
     * Description:
     * <删除用户,并移除其所有权限>
     * @author zhangwenyi
     * @date 11:22 2019/8/7
     * @param id 1
     * @return JsonResult
     **/
    @DeleteMapping("deleteUser/{id}")
    @ApiOperation(value = "删除用户,并移除其所有权限",notes = "删除用户,并移除其所有权限")
    @ApiImplicitParam(name = "id",value = "用户主键",dataType = "Long",paramType = "path",required = true)
    public JsonResult<Long> deleteUser(@PathVariable("id") Long id){
        userService.deleteUser(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, id);
    }

    /**
     * Description:
     * <禁用/启用用户>
     * @author pig
     * @date 11:23 2019/8/7
     * @param disableOrEnableUserVo 1
     * @return JsonResult
     **/
    @PostMapping("disableOrEnableUser")
    @ApiOperation(value = "禁用/启用用户",notes = "禁用/启用用户")
    @ApiImplicitParam(name = "disableOrEnableUserVo",value = "启用或禁用用户实体类",dataType = "DisableOrEnableUserVo",paramType = "body",required = true)
    public JsonResult<User> disableOrEnableUser(@RequestBody @Valid DisableOrEnableUserVo disableOrEnableUserVo){

        User user = new User();
        BeanUtils.copyProperties(disableOrEnableUserVo,user);

        userService.disableOrEnableUser(user);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, user);
    }

   /**
    * Description:
    * <更改用户状态>
    *
    * @param updateUserVo 1
    * @return ${package}.common.bean.vo.JsonResult
    * @author ZMF
    * @date 16:37 2021/3/16
    **/
    @PutMapping("userStatus")
    @ApiOperation(value = "更改用户状态",notes = "更改用户状态")
    @ApiImplicitParam(name = "updateUserVo",value = "更新用户实体类",dataType = "UpdateUserVo",paramType = "body",required = true)
    public JsonResult<String> updateUserStatus(@RequestBody @Valid UpdateUserVo updateUserVo){
        userService.updateUserStatus(updateUserVo);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS);
    }

    /**
     * Description:
     * <根据id查询用户信息>
     * @author wupanhua
     * @date 11:23 2019/8/7
     * @param id 1
     * @return JsonResult
     **/
    @GetMapping("queryUserById")
    @ApiOperation(value = "根据id查询用户信息",notes = "根据id查询用户信息")
    @ApiImplicitParam(name = "id",value = "用户主键",dataType = "Long",paramType = "query",required = true)
    public JsonResult<User> queryUserById(Long id){
        User user = userService.queryUserById(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,user);
    }

    /**
     * Description:
     * <根据id更新用户信息>
     * @author pig
     * @date 11:24 2019/8/7
     * @param updateUserVo 1
     * @return JsonResult
     **/
    @PostMapping("updateUserById")
    @ApiOperation(value = "根据id更新用户信息",notes = "根据id更新用户信息")
    @ApiImplicitParam(name = "updateUserVo",value = "更新用户实体类",dataType = "UpdateUserVo",paramType = "body",required = true)
    public JsonResult<User> updateUserById(@RequestBody @Valid UpdateUserVo updateUserVo){

	    userService.updateUserById(updateUserVo);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS);
    }

    /**
     * Description:
     * <根据主键重置用户密码>
     * @author pig
     * @date 11:24 2019/8/7
     * @param keys 1
     * @return JsonResult
     **/
    @PostMapping("resetUserPassBiKey")
    @ApiOperation(value = "根据主键重置用户密码",notes = "根据主键重置用户密码")
    @ApiImplicitParam(name = "keys",value = "用户主键列表",dataType = "Long",paramType = "body",required = true,allowMultiple = true)
    public JsonResult<Integer> resetUserPassBiKey(@RequestBody Long[] keys){
        // 对传入的值校验
        userService.resetUserPassByKey(keys);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, keys.length);
    }

    /**
     * Description:
     * <批量删除用户>
     * @author pig
     * @date 11:24 2019/8/7
     * @param keys 1
     * @return JsonResult
     **/
    @PostMapping("removeUsersByKeys")
    @ApiOperation(value = "批量删除用户",notes = "批量删除用户")
    @ApiImplicitParam(name = "keys",value = "用户主键列表",dataType = "Long",paramType = "body",required = true,allowMultiple = true)
    public JsonResult<Integer> removeUsersByKeys(@RequestBody List<Long> keys){
        userService.removeUsersByKeys(keys);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, keys.size());
    }

    /**
     * Description:
     * <查询账户是否存在>
     * @author pig
     * @date 11:24 2019/8/7
     * @param username 1
     * @param id 2
     * @return JsonResult
     **/
    @GetMapping("userVerifyUserId")
    @ApiOperation(value = "查询账户是否存在",notes = "查询账户是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "id",value = "用户主键",dataType = "Long",paramType = "query",required = true)})
    public JsonResult<Boolean> userVerifyUserId(@RequestParam("username") String username, @RequestParam("id") Long id){

        Boolean flag = userService.userVerifyUserId(username,id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,flag);
    }

    /**
     * Description:
     * <根据用户ID获取用户信息>
     * @author 吴盼华
     * @date 11:26 2019/8/7
     * @param user 1
     * @return JsonResult
     **/
    @ApiOperation(value = "根据用户ID获取用户信息",notes = "根据用户ID获取用户信息")
    @ApiImplicitParam(name = "user",value = "用户实体类",dataType = "User",paramType = "body",required = true)
    @PostMapping("findUserById")
    public JsonResult<User> findUserById(@RequestBody User user){
        user = userService.findUserById(user);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, user);
    }

    /**
     * Description:
     * <获取当前用户名>
     * @author wenxiaopeng
     * @date 18:37 2020/04/13
     * @return ${package}.common.bean.vo.JsonResult<java.lang.String>
     **/
    @GetMapping("/current")
    public JsonResult<ApiUser> currentUser(){
        User user = SessionUtil.getUser();
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, ApiUser.fromUser(user));
    }

	/**
	 * Description:
	 * <获取加密公钥>
	 *
	 * @return ${package}.common.bean.vo.JsonResult
	 * @author songcx
	 * @date 14:09 2020/6/29
	 **/
	@GetMapping("getKey")
	@ApiOperation(value = "获取加密公钥", notes = "获取加密公钥")
	public JsonResult getKey() throws Exception {
		return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, RsaUtil.getKey("public"));
	}
}
